package ads.corporativa.aula0204.dao.impl;

import static org.junit.Assert.*;

import org.junit.Test;

import ads.corporativa.aula0204.dao.ParticipanteDAO;
import ads.corporativa.aula0204.entity.Participante;
import ads.corporativa.aula0204.exceptions.MyException;

public class ParticipanteListDAOTest {

	@Test
	public void testInserir() {
		ParticipanteDAO dao = new ParticipanteListDAO();
		Participante p = new Participante("123", "paulo", "12345", "perfil");
		dao.save(p);
		assertEquals(1, dao.count());
	}

	@Test
	public void testAtualizar() {
		ParticipanteDAO dao = new ParticipanteListDAO();
		dao.save(new Participante("111", "paulo", "12345", "perfil"));
		dao.save(new Participante("222", "p1", "11111", "perfil"));
		Participante p = dao.findByCpf("111");
		p.setNome("Paulo Chaves");
		assertEquals(p.getNome(), dao.findByCpf("111").getNome());
	}

	@Test
	public void testDelete() {
		ParticipanteDAO dao = new ParticipanteListDAO();
		dao.save(new Participante("111", "paulo", "12345", "perfil"));
		dao.save(new Participante("222", "p1", "11111", "perfil"));
		dao.delete(dao.findByCpf("222").getId());
		assertEquals(1, dao.count());
	}

	@Test
	public void testFindByNome() {
		ParticipanteDAO dao = new ParticipanteListDAO();
		dao.save(new Participante("111", "paulo", "12345", "perfil"));
		dao.save(new Participante("222", "p1", "11111", "perfil"));
		dao.save(new Participante("333", "apa2", "11111", "perfil"));
		dao.save(new Participante("444", "efpa5", "11111", "perfil"));
		dao.save(new Participante("555", "eafi", "11111", "perfil"));
		assertEquals(3, dao.findByNome("pa").size());
	}

	@Test(expected = MyException.class)
	public void testGetByCPFInexistente() {
		ParticipanteDAO dao = new ParticipanteListDAO();
		dao.save(new Participante("111", "paulo", "12345", "perfil"));
		dao.save(new Participante("222", "p1", "11111", "perfil"));
		dao.findByCpf("333"); // não existe cpf 333
	}

	@Test(expected = MyException.class)
	public void testGetByIDInexistente() {
		ParticipanteDAO dao = new ParticipanteListDAO();
		dao.save(new Participante("111", "paulo", "12345", "perfil"));
		dao.save(new Participante("222", "p1", "11111", "perfil"));
		dao.find(20); // Não existe id 20
	}

	@Test(expected = MyException.class)
	public void testGetByNomeInexistente() {
		ParticipanteDAO dao = new ParticipanteListDAO();
		dao.save(new Participante("111", "paulo", "12345", "perfil"));
		dao.save(new Participante("222", "p1", "11111", "perfil"));
		dao.findByNome("zi"); // Não existe nome que contenha zi
	}
}
